import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/welcome',
    component: () => import('../components/Home.vue'),
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: () => import('../components/Welcome.vue')
      },
      {
        path: '/users',
        name: 'Users',
        component: () => import('../components/users/User.vue')
      },
      {
        path: '/rights',
        name: 'Rights',
        component: () => import('../components/power/Rights.vue')
      },
      {
        path: '/roles',
        name: 'Roles',
        component: () => import('../components/power/Roles.vue')
      },
      {
        path: '/categories',
        name: 'Categories',
        component: () => import('../components/goods/Cate.vue')
      },
      {
        path: '/params',
        name: 'Params',
        component: () => import('../components/goods/Params.vue')
      },
      {
        path: '/goods',
        name: 'GoodsList',
        component: () => import('../components/goods/List.vue')
      },
      {
        path: '/goods/add',
        name: 'Add',
        component: () => import('../components/goods/Add.vue')
      },
      {
        path: '/orders',
        name: 'Orders',
        component: () => import('../components/order/Order.vue')
      }
    ]
  }
  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home
  // },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router
